package com.daykw.douyin.task;

import java.util.Date;
import java.util.List;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;
import org.openqa.selenium.By;
import org.openqa.selenium.Cookie;
import org.openqa.selenium.WebDriver;

import com.daykw.douyin.config.NutzDaoConfig;
import com.daykw.douyin.http.HttpBase;
import com.daykw.douyin.utils.SpringContextHolder;

public class VideoLoginThread extends Thread {
	private static Logger logger = Logger.getLogger(VideoLoginThread.class);

	public void run() {
		try {
			this.doLogin();
		} catch (Exception e) {
			logger.error("", e);
		}
	}

	public void doLogin() throws Exception {
		NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
		List<Record> rs = dao.query("t_cookie",
				Cnd.wrap("where (status is null or status =0) and name!='大斌' ORDER BY RAND() LIMIT 1"));
		if (rs == null || rs.size() == 0) {
			logger.info("不用更新cookie");
			return;
		}
		Record r = rs.get(0);
		String user = r.getString("name");
		Long id = r.getLong("id");
		String password = r.getString("pwd");
		HttpBase http = new HttpBase();
		WebDriver wd = http.getFirefox();

		try {
			wd.get("https://dy.feigua.cn/");
			wd.manage().window().maximize();
			// alphalawyer
			// 0x010000008549222c03e05b0e560ebaf02eec1f9556bedd1cbca87cb59867485ab6fd6787c220305b1b6adbdd
			wd.findElement(By.cssSelector("a.login.hvr-ripple-out.js-douyin-login")).click();
			Thread.sleep(1000);
			wd.findElement(By.cssSelector("a.js-show-phone")).click();

			wd.findElement(By.cssSelector("input[name='tel']")).sendKeys(user);
			Thread.sleep(1000);
			wd.findElement(By.cssSelector("input[name='pwd']")).sendKeys(password);
			Thread.sleep(1000);
			wd.findElement(By.cssSelector("a.btn-login.js-account-logon")).click();
			Thread.sleep(1000);
			logger.info("登陸完畢");

			String html = wd.getPageSource();
			Document doc = Jsoup.parse(html);
			Elements wf = doc.select("p.warning-for");
			if (wf != null) {
				String txt = wf.text();
				if (StringUtils.isNotEmpty(txt) && txt.indexOf("您的账号存在异常风险") >= 0) {
					dao.update("t_cookie", Chain.make("cookie", null).add("auth_status", 9).add("status", 9)
							.add("create_time", new Date()), Cnd.where("id", "=", id));
					logger.info("账号被封:" + txt);

					return;
				}
			}

			Set<Cookie> cookies = wd.manage().getCookies();
			StringBuffer ck = new StringBuffer();
			for (Cookie cookie : cookies) {

				ck.append(cookie.getName() + "=" + cookie.getValue() + "; ");
			}

			if (ck.length() > 0) {
				ck = ck.deleteCharAt(ck.length() - 1);
			}

			dao.update("t_cookie", Chain.make("cookie", ck.toString()).add("auth_status", 1).add("status", 1)
					.add("create_time", new Date()), Cnd.where("id", "=", id));

			logger.info("cookie采集完毕,name：" + user + ",cookie=" + ck.toString());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			wd.close();
			wd.quit();
		}

	}
}
